/**
 * @author Phuc "Noel" Vo
 */
package com.example.noelwhereismystuffs.views.RegisterActivity;

import com.example.noelproject.R;
import com.example.noelwhereismystuffs.models.BackEndDatabase;
import com.example.noelwhereismystuffs.models.RegisterModel;
import com.example.noelwhereismystuffs.models.User;
import com.example.noelwhereismystuffs.presenters.RegisterPresenter;
import com.example.noelwhereismystuffs.views.SignInActivity.SignInActivity;
import com.example.noelwhereismystuffs.views.UserProfileActivity.UserProfileActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class RegisterActivity extends Activity implements IRegisterView {
	private RegisterPresenter myPresenter;
	private User myUser = User.NULL_USER;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		myPresenter = new RegisterPresenter(this, new RegisterModel(this));
		BackEndDatabase db = new BackEndDatabase(this);
	}

	/**
	 * Handle Register button
	 * @param source from xml file
	 */
	public void onRegisterClick(View source) {
		String email = getEmailField().getText().toString();
		String username = getUsernameField().getText().toString();
		String password = getPasswordField().getText().toString();
		myPresenter.onRegisterClick(username, email, password);
	}

	/**
	 * Handle Cancel button
	 * @param source from th xml file
	 */
	public void onCancelClick(View source) {
		myPresenter.onCancelClick();
	}
	
	/**
	 * Get information from Password Field
	 * @return
	 * 			EditText email
	 */
	private EditText getPasswordField() {
		return (EditText) findViewById(R.id.PasswordRegister);
	}

	/**
	 * Get information from Email Field
	 * @return
	 * 			EditText email
	 */
	private EditText getEmailField() {
		return (EditText) findViewById(R.id.EmailRegister);
	}
	
	/**
	 * Get information from Username Field
	 * @return
	 * 			EditText email
	 */
	private EditText getUsernameField() {
		return (EditText) findViewById(R.id.UsernameRegister);
	}

	@Override
	public void advanceRegister() {
		// create an intent (a request for phone OS to do something)
		Intent intent = new Intent(this, UserProfileActivity.class);
		// now pass that to the phone OS and ask to start it
		startActivity(intent);
		// pass the user to the next activity
		intent.putExtra("User", myUser);

	}

	@Override
	public void advanceCancel() {
		// create an intent (a request for phone OS to do something)
		Intent intent = new Intent(this, SignInActivity.class);
		// now pass that to the phone OS and ask to start it
		startActivity(intent);

	}

	@Override
	public void setUser(User user) {
		myUser = user;
	}

}
